// 创建路由器
// 模块化
import Vue from 'vue'
import VueRouter from 'vue-router'
//明确安装路由功能
Vue.use(VueRouter)
// 创建路由模块
const router = new VueRouter({
  // 使用routes进行路由配置
  routes:[
    // login的路由配置
    {
      name:'login',
      path:'/login',
      component:() =>import('@/views/login.vue')
    },
    // 注册register的路由配置
    {
      name:'register',
      path:'/register',
      component:()=>import('@/views/register.vue')
    },
    {
      name:'personal',
      path:'/personal/:id',
      component:()=>import('@/views/personal.vue')
    },
    {
      name:'personal',
      path:'/editUserInfo/:id',
      component:()=>import('@/views/editUserInfo.vue')
    }
  ]
})

// 添加全局的导航守卫--前置守卫，在跳转之前触发的
// 他的作用是限制用户的路由跳转  路由能否跳转 做出判断并进行处理
router.beforeEach((to, from, next) => {
//  let arr = ['/personal','/aa']
 if(to.path.indexOf('/personal') !== -1){
   let token = localStorage.getItem('toutiao_59_token')
   if(token){
     next()
   }else{
     next({name:'login'})
   }
 }else{
   next()
 }
})
// 暴露路由器
export default router
